﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;

public partial class UserManagement : System.Web.UI.Page
{
    private static string strConn = ConfigurationManager.ConnectionStrings["LabConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        populateData();
        populateLab();
        populateJenis();
    }

    private void populateLab()
    {
        string query = "sp_namalab_list";
        DataTable dt = new DataTable();
        using (SqlDataAdapter da = new SqlDataAdapter(query, strConn))
        {
            da.Fill(dt);
        }
        lstLab.DataSource = dt;
        lstLab.DataBind();
    }

    private void populateJenis()
    {
        string query = "sp_list_jenisuser";
        DataTable dt = new DataTable();
        using (SqlDataAdapter da = new SqlDataAdapter(query, strConn))
        {
            da.Fill(dt);
        }
        lstJenis.DataSource = dt;
        lstJenis.DataBind();
    }

    private void populateData()
    {
        DataTable dt = new DataTable();
        using (SqlDataAdapter da = new SqlDataAdapter("sp_user_list", strConn))
        {
            da.Fill(dt);
        }
        userList.DataSource = dt;
        userList.DataBind();
    }

    private void showMessage(string caption, string message, string type)
    {
        message = message.Replace("'", "&apos;");
        message = message.Replace(Environment.NewLine, "<br />");
        string clientScript = "$(document).ready(function(){ new PNotify({ title: '" + caption + "', text: '" + message + "', type: '" + type + "',styling: 'bootstrap3' }); });";
        ScriptManager.RegisterStartupScript(Page, this.GetType(), "ShowMessage", clientScript, true);
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        using (SqlConnection cn = new SqlConnection(strConn))
        {
            cn.Open();
            string query = "sp_tambah_user '{0}', '{1}', {2}, '{3}', '{4}', {5}";
            string sql = string.Format(query, username.Value, password.Value, Request.Form["jenis"], nama.Value, nip.Value, Request.Form["lab"]);
            using (SqlCommand cmd = new SqlCommand(sql, cn))

            //using (SqlCommand cmd = new SqlCommand("sp_tambah_lab '" + kemasan + "'", cn))
            {
                try
                {
                    int ra = cmd.ExecuteNonQuery();
                    if (ra > 0)
                    {
                        //this.namaLab.Value = string.Empty;
                        showMessage("Sukses Menambahkan User", "Nama <b>" + username.Value + "</b> berhasil ditambahkan.", "success");
                        username.Value = string.Empty;
                        nama.Value = string.Empty;
                        password.Value = string.Empty;
                        nip.Value = string.Empty;

                        populateData();
                    }
                    else
                    {
                        showMessage("Gagal Menambahkan User", "Tidak ada data yang ditambahkan", "error");
                    }
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                    if (msg.Contains("Cannot insert duplicate key"))
                    {
                        msg = "User '" + nama + "' sudah pernah terdaftar." + sql;
                    }
                    showMessage("Gagal Menambahkan User", msg, "error");
                }
            }
        }
    }
}